package com.example.myapplication;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class BaseActivity extends Activity {
    private final String ServerIP = "192.168.124.34";
    private static int ServerPort = 8087;

    private DBUtils mysql_db = null;
    private TCPUtils tcp_connect = null;
    public ExecutorService exec = Executors.newCachedThreadPool();
    public Handler uiHandler = new Handler();

    private TextView text_s1_val;
    private TextView text_s2_val;
    private TextView text_flag1;
    private TextView text_flag2;
    private EditText edit_s1_th;
    private EditText edit_s2_th;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View view =this.getLayoutInflater().inflate(R.layout.activity_main,null);
        setContentView(view);

        Button bt1 = (Button) findViewById(R.id.button1);
        Button bt2 = (Button) findViewById(R.id.button2);
        Button bt4 = (Button) findViewById(R.id.button4);
        Button bt5 = (Button) findViewById(R.id.button5);
        Button bt_search_1 = (Button) findViewById(R.id.search_1);
        Button bt_search_2 = (Button) findViewById(R.id.search_2);
        Button bt_search_3 = (Button) findViewById(R.id.search_3);
        Button bt_search_4 = (Button) findViewById(R.id.search_4);
        Button bt_set_th = (Button) findViewById(R.id.button_set_th);
        text_s1_val = (TextView) findViewById(R.id.text_s1_val);
        text_s2_val = (TextView) findViewById(R.id.text_s2_val);
        text_flag1 = (TextView) findViewById(R.id.text_flag1);
        text_flag2 = (TextView) findViewById(R.id.text_flag2);
        edit_s1_th = (EditText) findViewById(R.id.edit_s1_th);
        edit_s2_th = (EditText) findViewById(R.id.edit_s2_th);

        bt1.setOnClickListener(new ButtonListener());
        bt4.setOnClickListener(new ButtonListener());
        bt_search_1.setOnClickListener(new ButtonListener());
        bt_search_2.setOnClickListener(new ButtonListener());
        bt_search_3.setOnClickListener(new ButtonListener());
        bt_search_4.setOnClickListener(new ButtonListener());
        bt_set_th.setOnClickListener(new ButtonListener());

        // 连接MySQL数据库
        mysql_db = new DBUtils(uiHandler,view);
        exec.execute(mysql_db);
        // 连接TCP服务器
        tcp_connect = new TCPUtils(ServerIP,ServerPort);
        exec.execute(tcp_connect);
    }

    // 监听按钮点击事件
    private class ButtonListener implements View.OnClickListener {
        @SuppressLint("NonConstantResourceId")
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button1:
                    exec.execute(new Runnable() {
                        @Override
                        public void run() {
                            tcp_connect.send("#flag1|0#");
                        }
                    });
                    break;
                case R.id.button4:
                    exec.execute(new Runnable() {
                        @Override
                        public void run() {
                            tcp_connect.send("#flag1|1#");
                        }
                    });
                    break;
                case R.id.button_set_th:
                    exec.execute(new Runnable() {
                        @Override
                        public void run() {
                            String cmd = "#s_th|" + edit_s1_th.getText().toString() + "|" + edit_s2_th.getText().toString() + "#";
                            System.out.println("发送命令：" + cmd);
                            tcp_connect.send(cmd);
                        }
                    });
                    break;
                case R.id.search_1:
//                    Show_Table dialog_table_1 = new Show_Table(BaseActivity.this);
                    Show_Table dialog_table_1 = new Show_Table(BaseActivity.this,exec,mysql_db,1);
                    dialog_table_1.show();
                    break;
                case R.id.search_2:
                    Show_Table dialog_table_2 = new Show_Table(BaseActivity.this,exec,mysql_db,2);
                    dialog_table_2.show();
                    break;
                case R.id.search_3:
                    Show_Chart dialog_chart_1 = new Show_Chart(BaseActivity.this,exec,mysql_db,1);
                    dialog_chart_1.show();
                    break;
                case R.id.search_4:
                    Show_Chart dialog_chart_2 = new Show_Chart(BaseActivity.this,exec,mysql_db,2);
                    dialog_chart_2.show();
                    break;
            }
        }
    }
}
